<?php
namespace api\user\validate;

use think\Validate;

class RegisterValidate extends Validate
{
    protected $rule = [
            // 'username'          => 'require|checkUsername',
            'mobile'            => 'require|checkMobile',
            'password'          => 'require',//|checkPassword
            'confirm_password'  => 'require|confirm:password',
            //'trader_password'   => 'require|checkTraderPassword',
            //'confirm_trader_password'   => 'require|confirm:trader_password',
            //'pusername'         => 'require|checkPusername',
            'verification_code' => 'require|checkVerificationCode',
            'invite_code'       => 'require',
    ];
    protected $message = [
            //'username.require'          => '请输入用户名',
            'mobile.require'            => '请输入手机号',
            'password.require'          => '请输入您的密码',
            'confirm_password.require'  => '请确认密码',
            'confirm_password.confirm'  => '两次密码不一致',
            'verification_code.require' => '请输入验证码',
            'invite_code.require'       => '请输入邀请码',
            //'trader_password.require'   => '请输入您的交易密码',
            //'confirm_trader_password.require'   => '请确认交易密码',
            //'confirm_trader_password.confirm'  => '两次交易密码不一致',
            //'pusername.require'         => '请输入推荐人',
            
    ];

    protected $scene = [
    ];

    protected function checkUsername($value, $rule, $data)
    {
        if ((preg_match('/^[a-zA-Z0-9]{4,10}$/', $value))){
            return true;
        }else{
            return '用户名4-10位非中文';
        }
    }

    protected function checkMobile($value, $rule, $data)
    {
        if (preg_match('/^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/', $value)){
            return true;
        }else{
            return '手机号格式不正确!';
        }
    }

    protected function checkPassword($value, $rule, $data)
    {
       if ((preg_match('/^[a-zA-Z0-9]{4,10}$/', $value))){
            return true;
        }else{
            return '密码4-10位非中文';
        }
    }

    protected function checkTraderPassword($value, $rule, $data)
    {
        if (preg_match('/^\d{6}$/', $value)){
            return true;
        }else{
            return '交易密码格式不正确';
        }
    }

    protected function checkPusername($value, $rule, $data)
    {
        if ((preg_match('/^[a-zA-Z0-9]{4,10}$/', $value))){
            return true;
        }else{
            return '推荐人4-10位非中文';
        }
    }

    protected function checkVerificationCode($value, $rule, $data)
    {
        if (preg_match('/^\d{6}$/', $value)){
            return true;
        }else{
            return '验证码格式不正确';
        }
    }
}